package com.wcgksoft.controller.system;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.SysMenu;
import com.wcgksoft.entity.system.SystemVersion;
import com.wcgksoft.service.SysMenuService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.SystemVersionService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName SystemVersionController.java
 * @Description 版本管理控制器
 * @createTime 2020年03月06日 15:20:00
 */

@Controller
@RequestMapping("version")
public class SystemVersionController {
  @Autowired
  private SystemVersionService systemVersionService;

  @Autowired
  private SysRoleService sysRoleService;
  @Autowired
  private SysMenuService sysMenuService;

  /**
   * @throws
   * @title TODO
   * @description 进入版本列表页
   * @author 阿良
   * @param: page
   * @param: limit
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "list")
  public Layui findAll(
          @RequestParam(value = "page", required = false) Integer page,
          @RequestParam(value = "limit", required = false) Integer limit,
          @RequestParam(value = "term",required = false,defaultValue = "") String search_term) {

    DetachedCriteria criteria = DetachedCriteria.forClass(SystemVersion.class);
    if (!search_term.isEmpty()) {
      criteria.add(Restrictions.eq("versioname", search_term));
    }
    List<SystemVersion> list = systemVersionService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", systemVersionService.getTotalCount(), list);

  }


  /**
   * 添加
   */
  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data,
                      @RequestParam(value = "meun", required = false) Integer[] meun) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      SystemVersion systemVersion = (SystemVersion) JSONObject.toBean(JSONObject.fromObject(data), SystemVersion.class);
      systemVersion.setAddtime(new Date());
      Set<SysMenu> objects = new HashSet<>();
      for (Integer meuni : meun) {
        objects.add(new SysMenu(meuni));
      }
      systemVersion.setSysMenuSet(objects);
      systemVersionService.saveOrUpdate(systemVersion);
      return Layui.data(0, "保存成功", 1, Layui.addList(systemVersion));
    }
  }


  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "delete")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    System.out.println("id" + id);
    SystemVersion byId = systemVersionService.findById(id);
    systemVersionService.deleteCass(byId);

    return Layui.data(1, "删除成功", 1, null);
  }


  /**
   * 查询菜单权限数据
   *
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "menu")
  @Transactional
  public Layui menu() {
    DetachedCriteria criterias = DetachedCriteria.forClass(SysMenu.class);
    criterias.add(Restrictions.eq("parentId", -1));
    List<SysMenu> list = sysMenuService.findByConditions(criterias);
    for (SysMenu menu : list) {
      DetachedCriteria criteria = DetachedCriteria.forClass(SysMenu.class);
      criteria.add(Restrictions.eq("parentId", menu.getId()));
      List<SysMenu> lists = sysMenuService.findByConditions(criteria);
      menu.setSysMenus(lists);
    }
    return Layui.data(1, "查询成功", 1, list);
  }


  /**
   * @throws
   * @title TODO
   * @description 提供下拉框接口
   * @author 阿良
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "json_version_list")
  public Layui findAll() {
    List<SystemVersion> list = systemVersionService.findAll(SystemVersion.class);
    return Layui.data(0, "查询成功", systemVersionService.getTotalCount(), list);

  }


}
